﻿Public Class perStatus
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Propriedades"

    Public ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String
            sSql = "SELECT IDStatus, Descricao "
            sSql &= "  FROM DC_Status "
            sSql &= "ORDER BY Descricao"

            Return sSql
        End Get
    End Property

#End Region

#Region "Métodos"

    Public Function inserirStatus(ByVal sDescricao As String, _
                                  ByVal iPeriodicidade As Int32, _
                                  ByVal iTolerancia As Int32, _
                                  ByVal bTornaInativo As Boolean) As Int32

        Dim iIdStatus As Int32
        Dim sSql As String

        Try
            iIdStatus = Me.objBuscaId.BuscaID("IDStatus", "DC_Status")

            sSql = "  INSERT INTO DC_Status "
            sSql &= " ( "
            sSql &= "   IDStatus, "
            sSql &= "   Descricao, "
            sSql &= "   Periodicidade, "
            sSql &= "   Tolerancia, "
            sSql &= "   TornaInativo "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= " ( "
            sSql &= iIdStatus & ", "
            sSql &= "'" & sDescricao & "', "
            sSql &= iPeriodicidade & ", "
            sSql &= iTolerancia & ","
            sSql &= CInt(bTornaInativo)
            sSql &= " ) "

            MyBase.executarAcao(sSql)
            Return iIdStatus
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarStatus(ByVal iIdStatus As Int32, _
                             ByVal sDescricao As String, _
                             ByVal iPeriodicidade As Int32, _
                             ByVal iTolerancia As Int32, _
                             ByVal bTornaInativo As Boolean)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_Status SET "
            sSql &= "   Descricao = '" & sDescricao & "', "
            sSql &= "   Periodicidade = " & iPeriodicidade & ", "
            sSql &= "   Tolerancia = " & iTolerancia & ","
            sSql &= "   TornaInativo = " & CInt(bTornaInativo)
            sSql &= " WHERE "
            sSql &= "   IdStatus = " & iIdStatus

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirStatus(ByVal iIdStatus As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Status "
            sSql &= " WHERE "
            sSql &= "   IDStatus = " & iIdStatus

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoStatus(ByVal sCampo As String, _
                                          ByVal iIDStatus As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_Status "

            If (iIDStatus > 0) Then
                sSql &= " WHERE IDStatus = " & iIDStatus
            End If

            dsDados = MyBase.executarConsulta(sSql, "DC_Status")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Status").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Status")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarStatus(ByVal iIdStatus As Int32) As DataRow
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Status "
            sSql &= " WHERE IDStatus = " & iIdStatus

            dsDados = MyBase.executarConsulta(sSql, "DC_Status")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Status").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Status").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
#End Region
End Class
